<template>
	<view class="">
		<!-- {{item}} -->

		<u-form-item :label="item.label" :prop="item.value"
			:borderBottom="item.iscard != 'after' && item.iscard != 'card'"
			:labelPosition="(item.attrs && item.attrs.labelPosition) || 'left'" :required="item.required">
			<!-- {{ uForm.disabled }} -->
			<!-- {{item.dw}} -->
			<view class="u-flex">
				<u--textarea v-if="item.type == 'textarea'" placeholder="请输入内容" v-model="inputValue" border="none"
					:disabled="disabled" @blur="blur"></u--textarea>
				<u--input v-else placeholder="请输入内容" :type="item.type || 'text'" v-model="inputValue" border="none"
					:readonly="disabled" @blur="blur">

				</u--input>
				<view class="icon" v-if="!disabled" @click="getLocation()" style="margin-right: 20rpx;">
					<u-image :src="wz" width="40rpx" height="40rpx" mode="aspectFit"></u-image>
				</view>
			</view>
		</u-form-item>
	</view>
</template>

<script>
	import wz from "@/static/map/wz.png"
	export default {
		mixins: [uni.$u.mpMixin, uni.$u.mixin],
		props: {
			item: {
				typeof: Object,
				default: () => {
					return {};
				},
			},
			data: {
				typeof: Object,
				default: () => {
					return {};
				},
			},
			value: {
				typeof: [String, Number, Boolean, Array, Object],
				default: () => {
					return "";
				},
			},
		},
		data() {
			return {
				wz,
				inputValue: "",
			};
		},
		watch: {
			value: {
				handler(n, o) {
					let value = this.value;
					let item = this.item;
					if (!item.value) {
						value = item.defaultValue || "";
					}

					if (item.fl && value && Number(value).div(Number(item.fl)) != 0) {
						value = Number(value).div(Number(item.fl));
					}
					// state.inputValue = value
					this.inputValue = value;
				},
				immediate: true,
				deep: true,
			},
		},
		computed: {
			disabled() {
				this.getParentData("u-form")
				return this.parent.disabled;
			},
		},
		methods: {
			blur() {
				let item = this.item;
				let value = this.inputValue;
				if (item.fl && value && Number(value).div(Number(item.fl)) != 0) {
					value = String(parseInt(Number(value).mul(Number(item.fl))));
				}
				this.$emit("input", value);
			},
			getLocation() {
				const form = uni.$u.$parent.call(this, 'u-form')
				let latlng = (this.data.coordinate || '').split(",")
				if (latlng.length == 2) {
					uni.chooseLocation({
						latitude: Number(latlng[1]),
						longitude: Number(latlng[0]),
						success: (res) => {
							this.$emit("change", {
								coordinate: `${res.latitude},${res.longitude}`
							});
							this.$emit("input", res.name);
							uni.$u.formValidate(this, "blur");
							form.validateField(this.item.value)
						}
					})

					return
				}
				uni.chooseLocation({

					success: (res) => {
						this.$emit("change", {
							coordinate: `${res.longitude},${res.latitude}`
						});
						this.$emit("input", res.name);
						uni.$u.formValidate(this, "blur");
					},
					fail: function(res) {
						console.log(res);
					}
				});
			}
		},
	};
</script>

<style lang="scss" scoped>
	.dw {
		padding: 0 20rpx;
		color: #2093f5;
	}
</style>